package com.andnux.android.adapter;

import android.view.View;

import com.andnux.android.MainApp;
import com.andnux.android.data.ChatSession;
import com.andnux.android.data.SystemUsers;
import com.andnux.android.data.Works;
import com.andnux.android.databinding.ItemSessionBinding;
import com.andnux.android.network.GsonUtils;
import com.andnux.android.utils.DateUtils;
import com.andnux.android.utils.ImageLoader;
import com.andnux.android.utils.StringUtils;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class SessionAdapter extends BaseAdapter<ChatSession, ItemSessionBinding> {

    private final SystemUsers users;

    public SessionAdapter() {
        users = MainApp.getApp().getSystemUsers();
    }

    @Override
    public void onBindView(ItemSessionBinding itemSessionBinding, ChatSession chatSession, int position) {
        super.onBindView(itemSessionBinding, chatSession, position);
        if (StringUtils.toString(users.getId()).contentEquals(chatSession.getToId())) {
            itemSessionBinding.title.setText(chatSession.getFromUser().getNickname());
            ImageLoader.load(chatSession.getFromUser().getAvatar(), itemSessionBinding.hedImage);
        } else {
            itemSessionBinding.title.setText(chatSession.getToUser().getNickname());
            ImageLoader.load(chatSession.getToUser().getAvatar(), itemSessionBinding.hedImage);
        }
        itemSessionBinding.content.setText(chatSession.getMessage());
        itemSessionBinding.time.setText(DateUtils.format(chatSession.getCreateTime(), DateUtils.DATE_TIME_PATTERN));
        Works works = chatSession.getWorks();
        String json = works.getFiles();
        List<String> files = GsonUtils.gson.fromJson(json, new TypeToken<List<String>>() {
        }.getType());
        ImageLoader.load(files.get(0), itemSessionBinding.worksImage);
        itemSessionBinding.tvMsgCount.setVisibility(chatSession.getToBeReadMessCount() > 0 ? View.VISIBLE : View.GONE);
        itemSessionBinding.tvMsgCount.setText(StringUtils.toString(chatSession.getToBeReadMessCount()));
    }
}
